package system;

import java.io.IOException;

import java.sql.Timestamp;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.event.ActionEvent;

import oracle.adf.view.rich.component.rich.data.RichTable;
import oracle.adf.view.rich.component.rich.input.RichInputDate;
import oracle.adf.view.rich.component.rich.input.RichInputText;
import oracle.adf.view.rich.component.rich.input.RichSelectOneChoice;
import oracle.adf.view.rich.context.AdfFacesContext;

import oracle.stellent.ridc.IdcClientException;

import view.common.Utils;

public class SystemLog {

    private ManageWebCenterContent obj;

    public SystemLog() {
        if (Boolean.FALSE.equals(Utils.getElExpression("#{adfFacesContext.postback}"))) {
            try {
                bindData();
            } catch (Exception e) {
                Utils.showMessage(Utils.MSG_ERROR, e.getMessage());
            }
        }
    }

    public void bindData() throws IdcClientException, IOException {
        List<Map<String, Object>> lst = new ArrayList<Map<String, Object>>();

        String where = "";
        if (Utils.getPageFlowScope("whereclause") != null) {
            where = (String)Utils.getPageFlowScope("whereclause");
            String order = " order by dActionDate desc";
            obj = new ManageWebCenterContent();
            lst = obj.docHistory(where, order);
        }

        table.setValue(lst);
        AdfFacesContext.getCurrentInstance().addPartialTarget(table);
    }

    public void filterData(ActionEvent actionEvent) {
        try {
            getSearchCondition();
            bindData();
        } catch (Exception e) {
            Utils.showMessage(Utils.MSG_ERROR, e.getMessage());
        }
    }

    private void getSearchCondition() {
        String where = "";
        String strActor = "";
        String straction = "";
        Timestamp timeFrom = null;
        Timestamp timeTo = null;

        if (actor.getValue() != null && (String)actor.getValue() != "") {
            strActor = (String)actor.getValue();
            if (where != "") {
                where = " AND dUser = '" + strActor + "' ";
            } else {
                where = where + " dUser = '" + strActor + "' ";
            }
        }

        if (!action.getValue().equals("0")) {
            straction = (String)action.getValue();
            if (where != "") {
                where = where + " AND dAction = '" + straction + "' ";
            } else {
                where = "dAction = '" + straction + "' ";
            }
        }

        if (from.getValue() != null) {
            timeFrom =
                    Utils.toTimeStamp(Utils.getFormatDateByObject(from.getValue()));
            if (where != "") {
                where = where + " AND dActionDate > {ts '" + timeFrom + "'}";
            } else {
                where = "dActionDate > {ts '" + timeFrom + "'}";
            }
        }

        if (to.getValue() != null) {
            timeTo =
                    Utils.toTimeStamp(Utils.getFormatDateByObject(to.getValue()));
            if (where != "") {
                where = where + " AND dActionDate < {ts '" + timeTo + "'}";
            } else {
                where = "dActionDate < {ts '" + timeTo + "'}";
            }
        }

        Utils.setPageFlowScope("whereclause", where);
    }

    private RichTable table = new RichTable();
    private RichSelectOneChoice action = new RichSelectOneChoice();
    private RichInputDate from = new RichInputDate();
    private RichInputDate to = new RichInputDate();
    private RichInputText actor = new RichInputText();

    public void setTable(RichTable table) {
        this.table = table;
    }

    public RichTable getTable() {
        return table;
    }


    public void setAction(RichSelectOneChoice action) {
        this.action = action;
    }

    public RichSelectOneChoice getAction() {
        return action;
    }

    public void setFrom(RichInputDate from) {
        this.from = from;
    }

    public RichInputDate getFrom() {
        return from;
    }

    public void setTo(RichInputDate to) {
        this.to = to;
    }

    public RichInputDate getTo() {
        return to;
    }

    public void setActor(RichInputText actor) {
        this.actor = actor;
    }

    public RichInputText getActor() {
        return actor;
    }
}
